package com.nineclock.system.controller;

import com.nineclock.api.system.controller.SysOrganizationControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.dto.SysCompanyUserDTO;
import com.nineclock.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/organization")
public class SysOrganizationController implements SysOrganizationControllerApi {
    @Autowired
    SysOrganizationService sysOrganizationService;
    // 查询组织下所有员工
    @GetMapping("/members/simple")
    @Override
    public Result<List<SysCompanyUserDTO>> findAllMembers() {
        List<SysCompanyUserDTO> members = sysOrganizationService.findAllMembers();
        return Result.success(members);
    }

    /**
     * 获取组织架构列表
     * @return
     */
    @GetMapping("/department")
    @Override
    public Result<List<SysDepartmentDTO>> findDepartment() {
        List<SysDepartmentDTO> sysDepartmentDTOS = sysOrganizationService.findDepartment();
        return Result.success(sysDepartmentDTOS);
    }

    /**
     * 查询部门成员列表
     * @param departmentId
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/members")
    @Override
    public Result<PageResult<SysCompanyUserDTO>> findDepartmentUser(
            @RequestParam(value = "departmentId",defaultValue = "0") Long departmentId
            ,@RequestParam(defaultValue = "1",value = "page") Integer page
            ,@RequestParam(defaultValue = "10",value = "pageSize") Integer pageSize
            ,@RequestParam(required = false,value = "keyword") String keyword) {
        PageResult<SysCompanyUserDTO> pageResult = sysOrganizationService.findDepartmentUser(departmentId, page, pageSize, keyword);
        return Result.success(pageResult);
    }
// 获取当前企业所对应的所有部门
    @GetMapping("/findDepartmentByCompanyId")
    @Override
    public Result<List<Long>> findDepartmentByCompanyId() {
        List<Long> counts = sysOrganizationService.findDepartmentByCompanyId();
        return Result.success(counts);
    }
    // 查询考勤组下的部门员工
    @GetMapping("/findAttendAndCompanyUserCount")
    @Override
    public Result<Integer> findAttendAndCompanyUserCount(@RequestParam("departmentId") List<Long> departmentId) {
        Integer count = sysOrganizationService.findAttendAndCompanyUserCount(departmentId);
        return Result.success(count);
    }
// 查询部门简单列表
    @GetMapping("/simple")
    @Override
    public Result<List<DepartmentOrUserSimpleDTO>> findDepartmentSimple(
         /*  @RequestParam(value = "page",defaultValue = "1") Integer page
            ,@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize
            ,*/@RequestParam(value = "id") Long departmentId
            ,@RequestParam(value = "includeMember",defaultValue = "0") Integer includeMember) {
        List<DepartmentOrUserSimpleDTO> list = sysOrganizationService.findDepartmentSimple(departmentId, includeMember);
        return Result.success(list);
    }

    // 直接导入员工数据
    @PostMapping("/uploadExcel")
    @Override
    public Result uploadExcel(MultipartFile excelFile) {
        sysOrganizationService.uploadExcel(excelFile);
        return Result.success();
    }
// 根据员工ID , 依次获取其 所在部门的ID及上层部门ID
    @GetMapping("/findDepartmentIdByCompanyUserId/{companyUserId}")
    @Override
    public Result<List<Long>> findDepartmentIdByCompanyUserId(@PathVariable("companyUserId") Long companyUserId) {
        List<Long> list = sysOrganizationService.findDepartmentIdByCompanyUserId(companyUserId);
        return Result.success(list);
    }
}
